class Api::V1::CouponsController < Api::V1::BaseController
  skip_before_action :authenticate_request!, only: [:index, :categories]
  before_action :authenticate_request, only: [:index]

  def index
    @spu_coupons = SpuCoupon.joins(:coupon).where(coupons: { state: :usable }).where("coupons.end_at >= ?", Time.now.to_datetime)
    if params[:shop_category_id].presence
      @spu_coupons = @spu_coupons.where(coupons: { shop_category_id: params[:shop_category_id] })
    end
    @spu_coupons = @spu_coupons.order("created_at DESC").page(params[:page]).per(params[:per])
  end

  def categories
    #@categories = [{
    #  :id => 0,
    #  :name => '精选',
    #  :type => 'Chosen'
    #}]
    @categories = []
    @shop_categories = ShopCategory.where("parent_id = 0 OR parent_id IS NULL").order("sort ASC")
    @shop_categories.each do |shop_category|
      @categories.push({
        :id => shop_category.id,
        :name => shop_category.name,
        :type => shop_category.class.name
      })
    end
  end
end
